package pack;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import dao.BiletDAORemote;
import dao.EmployeeDAORemote;
import dto.BiletDTO;
import dto.EmployeeDTO;

@ManagedBean(name = "biletBean")
@SessionScoped
// @RequestScoped era dar nu merge asa...tre sa schimb aici ceva
public class BiletBean implements Serializable {

	/**
	 *    
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	BiletDAORemote biletDAO;
	
	@EJB
	EmployeeDAORemote employeeDAO;

	@ManagedProperty(value = "#{biletDTO}")
	private BiletDTO biletDTO;
	
	EmailBean email = new EmailBean();

	ExternalContext exCon;
	private int id;
	private int empl_id;
	private String status;
	private String motiv;
	private int tl_id;
	private String firstname;
	private String lastname;
	private String start_an;
	private String start_ora;
	private String start_min;
	private String end_an;
	private String end_ora;
	private String end_min;
	
	private String teaml;

	private int biletId;

	private String butonId;
	
	private String dupa;
	
	private String valoare;
	
	private String hidd;



	List<BiletDTO> dupaFiltru;

	List<BiletDTO> bilete; // = biletDAO.findAllTickets(eDTO);

	
	public void resetFields() {
		setMotiv("");
		setStart_an("2013-01-01");
		setStart_ora("08");
		setStart_min("00");
		setEnd_an("2013-01-01");
		setEnd_ora("08");
		setEnd_min("00");
	}

	public void verify() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 5) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
		//read();
		filtru();
	}
	
	public void verifyTL(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO)fc.getExternalContext().getSessionMap().get("loggedInUser");
		
		ConfigurableNavigationHandler nav 
			= (ConfigurableNavigationHandler) 
		fc.getApplication().getNavigationHandler();
		
		if (eDTO != null){
			if (eDTO.getRank() != 3){
				nav.performNavigation("home.xhtml");
			}
		}
		else {
			nav.performNavigation("home.xhtml");
		}
		
		getAllBilete();
	}

	public String read() {
		EmployeeDTO eDTO = new EmployeeDTO();
		FacesContext fc = FacesContext.getCurrentInstance();
		eDTO = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		bilete = biletDAO.findAllTickets2(eDTO);
		this.setBilete(bilete);
		EmployeeDTO e = new EmployeeDTO();
		FacesContext fc2 = FacesContext.getCurrentInstance();
		e = (EmployeeDTO) fc2.getExternalContext().getSessionMap().get("tl_id");
		setTeaml(e.getLastname() + e.getFirstname());
		return "";
	}

	public String getAllBilete() {
		EmployeeDTO eDTO = new EmployeeDTO();
		FacesContext fc = FacesContext.getCurrentInstance();
		try {
			eDTO = (EmployeeDTO) fc.getExternalContext().getSessionMap()
					.get("loggedInUser");
			setBilete(biletDAO.findAllTickets(eDTO));
			this.setBilete(bilete);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";

	}

	public String send() {
		EmployeeDTO ee = new EmployeeDTO();
		FacesContext fc = FacesContext.getCurrentInstance();
		ee = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		biletDTO = new BiletDTO();
		biletDTO.setStatus("In asteptare");
		biletDTO.setEmpl_id(ee.getId());
		biletDTO.setMotiv(getMotiv());
		biletDTO.setStart_time(getStart_time());
		biletDTO.setEnd_time(getEnd_time());
		EmployeeDTO e = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("tl_id");
		biletDTO.setTl_id(e.getId());
		biletDTO.setFirstname(ee.getFirstname());
		biletDTO.setLastname(ee.getLastname());
		try {
			biletDAO.addBilet(biletDTO);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		//String from = biletDTO.g
		EmployeeDTO employee = new EmployeeDTO();
		employee = employeeDAO.getEmployeeById(biletDTO.getEmpl_id());
		EmployeeDTO teamleader = new EmployeeDTO();
		teamleader = employeeDAO.getEmployeeById(biletDTO.getTl_id());
		String to = teamleader.getEmail();
		String from = employee.getEmail();
		String subject = "Ticket of leaving request!";
		String msg = "Hi " + teamleader.getFirstname() + "!\n\n" 
				+ employee.getFirstname() + " " + employee.getLastname()
				+ " sent you a ticket of leaving with subject: "
				+ biletDTO.getMotiv() + ".\n\n"
				+ "Please respond as quick as you can...";
		email.sendMail(from, to, subject, msg);
		resetFields();
		return "";

	}

	public String aprova(BiletDTO b) {

		biletDAO.aprovaBiletById(b.getId());
		EmployeeDTO employee = new EmployeeDTO();
		employee = employeeDAO.getEmployeeById(b.getEmpl_id());
		String to = employee.getEmail();
		String from = employeeDAO.findAdminEmail();
		String subject = "Ticket of leaving request!";
		String msg = "Hi " + employee.getFirstname() + "!\n\n" 
				+ "Your request for ticket of leaving with the motiv: "
				+ b.getMotiv() + " was accepted." + ".\n\n";
		email.sendMail(from, to, subject, msg);
		return "";
	}

	public String refuza(BiletDTO b) {
		biletDAO.refuzaBiletById(b.getId());
		EmployeeDTO employee = new EmployeeDTO();
		employee = employeeDAO.getEmployeeById(b.getEmpl_id());
		String to = employee.getEmail();
		String from = employeeDAO.findAdminEmail();
		String subject = "Ticket of leaving request!";
		String msg = "Hi " + employee.getFirstname() + "!\n\n" 
				+ "Your request for ticket of leaving with the motiv: "
				+ b.getMotiv() + " was refused." + ".\n\n";
		email.sendMail(from, to, subject, msg);
		return "";
	}
	
	public String filtru(){
		read();
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");
		if ("Status".equals(getDupa())){
			if ("Refuzat".equals(getValoare())){
				setBilete(biletDAO.getBileteDupaStatus("Refuzat", eDTO.getId()));
			}
			else if ("Aprobat".equals(getValoare())){
				setBilete(biletDAO.getBileteDupaStatus("Aprobat", eDTO.getId()));
			}
			else if ("In asteptare".equals(getValoare())){
				setBilete(biletDAO.getBileteDupaStatus("In asteptare", eDTO.getId()));
			}
		}
		else if ("Data".equals(getDupa())){
			setBilete(biletDAO.getBileteDupaData(getValoare(), eDTO.getId()));
			try{
				if (getBilete().size() > 0)
					System.out.println("JOl ! ");
			}
			catch (NullPointerException e) {
				setHidd("error");
			}
		}
		else{
			System.out.println("Error! ");
		}
		return "";
	}

	public String getStart_time() {
		return this.setDateStart();
	}

	public void setStart_time(String start_time) {
	}

	public String getEnd_time() {
		return this.setDateEnd();
	}

	public void setEnd_time(String end_time) {
	}
	
	public List<BiletDTO> getBilete() {
		return bilete;
	}

	public void setBilete(List<BiletDTO> bilete) {
		this.bilete = bilete;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getEmpl_id() {
		return empl_id;
	}

	public void setEmpl_id(int empl_id) {
		this.empl_id = empl_id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getMotiv() {
		return motiv;
	}

	public void setMotiv(String motiv) {
		this.motiv = motiv;
	}

	public BiletDTO getBiletDTO() {
		return biletDTO;
	}

	public void setBiletDTO(BiletDTO biletDTO) {
		this.biletDTO = biletDTO;
	}


	public int getTl_id() {
		return tl_id;
	}

	public void setTl_id(int tl_id) {
		this.tl_id = tl_id;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public int getBiletId() {
		return biletId;
	}

	public void setBiletId(int biletId) {
		this.biletId = biletId;
	}

	public String getStart_an() {
		return start_an;
	}

	public void setStart_an(String start_an) {
		this.start_an = start_an;
	}

	public String getStart_ora() {
		return start_ora;
	}

	public void setStart_ora(String start_ora) {
		this.start_ora = start_ora;
	}

	public String getStart_min() {
		return start_min;
	}

	public void setStart_min(String start_min) {
		this.start_min = start_min;
	}

	public String getEnd_an() {
		return end_an;
	}

	public void setEnd_an(String end_an) {
		this.end_an = end_an;
	}

	public String getEnd_ora() {
		return end_ora;
	}

	public void setEnd_ora(String end_ora) {
		this.end_ora = end_ora;
	}

	public String getEnd_min() {
		return end_min;
	}

	public void setEnd_min(String end_min) {
		this.end_min = end_min;
	}

	public String setDateStart() {
		String dd = "";
		dd = dd + getStart_an() + "-" + getStart_ora() + ":" + getStart_min() + ":00.0";
		return dd;
	}

	public String setDateEnd() {
		String dd = "";
		dd = dd + getEnd_an() + "-" + getEnd_ora() + ":" + getEnd_min() + ":00.0";
		return dd;
	}

	public String getButonId() {
		return butonId;
	}

	public void setButonId(String butonId) {
		this.butonId = butonId;
	}

	public String getTeaml() {
		return teaml;
	}

	public void setTeaml(String teaml) {
		this.teaml = teaml;
	}

	public String getDupa() {
		return dupa;
	}

	public void setDupa(String dupa) {
		this.dupa = dupa;
	}

	public String getValoare() {
		return valoare;
	}

	public void setValoare(String valoare) {
		this.valoare = valoare;
	}

	public List<BiletDTO> getDupaFiltru() {
		return dupaFiltru;
	}

	public void setDupaFiltru(List<BiletDTO> dupaFiltru) {
		this.dupaFiltru = dupaFiltru;
	}

	public String getHidd() {
		return hidd;
	}

	public void setHidd(String hidd) {
		this.hidd = hidd;
	}
}
